function [pupil_d,pupil_pos]=get_pupil_data(idata)
tmp=mean(idata(:,:,2:10),3);
vect=double(reshape(tmp,size(tmp,1)*size(tmp,2),1));
thresh=mean(vect)+(2*std(vect));
thresh=round(thresh);

cutOff_perc=10;
cutOff_perc=cutOff_perc/100;
vert_lim=size(idata,2)*cutOff_perc;
vert_lim=round(vert_lim);
horz_lim=size(idata,1)*cutOff_perc;
horz_lim=round(horz_lim);

pupil_pos=[];
pupil_d=[];
pupil_pos(1,1)=round(size(idata,2)/2);
pupil_pos(2,1)=round(size(idata,1)/2);
pupil_d(1,1)=1;
pupil_d(2,1)=1;
pupil_d(3,1)=1;
pupil_d(4,1)=1;
offsets=[0 1 -1 2 -2 3 -3 4 -4; 0 2 -2 -1 1 4 -4 -3 3];
for ond=2:size(idata,3)-1
    v=sum(idata(horz_lim:end-horz_lim,vert_lim:end-vert_lim,ond)>thresh,1);
    tmp_v=sort(v,'descend');
    cur_v=[];
    for und=1:10
        cur_v(und)=find(v==tmp_v(und),1,'first');
    end
    vert=round(mean(cur_v));
    vert=vert+vert_lim-1;
    
    h=sum(idata(horz_lim:end-horz_lim,vert_lim:end-vert_lim,ond)>thresh,2);
    tmp_h=sort(h,'descend');
    cur_h=[];
    for und=1:10
        cur_h(und)=find(h==tmp_h(und),1,'first');
    end
    horz=round(mean(cur_h));
    horz=horz+horz_lim-1;
    try
        x1=[];
        x2=[];
        y1=[];
        y2=[];
        for jnd=1:9
            x1=find(idata(horz+offsets(1,jnd),vert+offsets(2,jnd):end,ond)<thresh,1,'first');
            x2=find(idata(horz+offsets(1,jnd),vert+offsets(2,jnd):-1:1,ond)<thresh,1,'first');
            y1=find(idata(horz+offsets(1,jnd):end,vert+offsets(2,jnd),ond)<thresh,1,'first');
            y2=find(idata(horz+offsets(1,jnd):-1:1,vert+offsets(2,jnd),ond)<thresh,1,'first');
        end
        
        x1=mean(x1);
        x2=mean(x2);
        y1=mean(y1);
        y2=mean(y2);
%         x1=find(idata(horz,vert:end,ond)<thresh,1,'first');
%         x2=find(idata(horz,vert:-1:1,ond)<thresh,1,'first');
%         y1=find(idata(horz:end,vert,ond)<thresh,1,'first');
%         y2=find(idata(horz:-1:1,vert,ond)<thresh,1,'first');
        pupil_pos(1,ond)=vert;
        pupil_pos(2,ond)=horz;
        pupil_d(1,ond)=x1;
        pupil_d(2,ond)=x2;
        pupil_d(3,ond)=y1;
        pupil_d(4,ond)=y2;
    catch
        pupil_pos(1,ond)=pupil_pos(1,ond-1);
        pupil_pos(2,ond)=pupil_pos(2,ond-1);
        pupil_d(1,ond)=pupil_d(1,ond-1);
        pupil_d(2,ond)=pupil_d(2,ond-1);
        pupil_d(3,ond)=pupil_d(3,ond-1);
        pupil_d(4,ond)=pupil_d(4,ond-1);
    end
end
% pupil_pos=[];
% pupil_d=[];
% for ond=1:size(idata,3)
%     v=sum(idata(horz_lim:end-horz_lim,vert_lim:end-vert_lim,ond)>thresh,1);
%     tmp_v=sort(v,'descend');
%     cur_v=[];
%     for und=1:10
%         cur_v(und)=find(v==tmp_v(und),1,'first');
%     end
%     vert=round(mean(cur_v));
%     vert=vert+vert_lim-1;
%
%     h=sum(idata(horz_lim:end-horz_lim,vert_lim:end-vert_lim,ond)>thresh,2);
%     tmp_h=sort(h,'descend');
%     cur_h=[];
%     for und=1:10
%         cur_h(und)=find(h==tmp_h(und),1,'first');
%     end
%     horz=round(mean(cur_h));
%     horz=horz+horz_lim-1;
%     if ond~=1
%         if sum(v)<200
%             pupil_pos(1,ond)=pupil_pos(1,ond-1);
%             pupil_pos(2,ond)=pupil_pos(2,ond-1);
%             pupil_d(1,ond)=pupil_d(1,ond-1);
%             pupil_d(2,ond)=pupil_d(2,ond-1);
%             pupil_d(3,ond)=pupil_d(3,ond-1);
%             pupil_d(4,ond)=pupil_d(4,ond-1);
%             blink_inds(ond)=ond;
%         else
%             x1=find(idata(horz,vert:end,ond)<thresh,1,'first');
%             x2=find(idata(horz,vert:-1:1,ond)<thresh,1,'first');
%             y1=find(idata(horz:end,vert,ond)<thresh,1,'first');
%             y2=find(idata(horz:-1:1,vert,ond)<thresh,1,'first');
%             pupil_pos(1,ond)=vert;
%             pupil_pos(2,ond)=horz;
%             pupil_d(1,ond)=x1;
%             pupil_d(2,ond)=x2;
%             pupil_d(3,ond)=y1;
%             pupil_d(4,ond)=y2;
%         end
%     else
%         pupil_pos(1,ond)=round(size(idata,2)/2);
%         pupil_pos(2,ond)=round(size(idata,1)/2);
%         pupil_d(1,ond)=1;
%         pupil_d(2,ond)=1;
%         pupil_d(3,ond)=1;
%         pupil_d(4,ond)=1;
%     end
% end
%
%
% for ond=1:size(idata,3)
%     new_idata=idata(:,:,ond)>thresh;
%     v=sum(new_idata(horz_lim:end-horz_lim,vert_lim:end-vert_lim),1);
%     h=sum(new_idata(horz_lim:end-horz_lim,vert_lim:end-vert_lim),2);
%
%
% %     v=sum(idata(horz_lim:end-horz_lim,vert_lim:end-vert_lim,ond),1);
% %     h=sum(idata(horz_lim:end-horz_lim,vert_lim:end-vert_lim,ond),1);
%
% %     v=sum(new_idata,1);
% %     h=sum(new_idata,2);
%
%     if sum(v)<5 | sum(v>0)<3
%         if ond~=1
%             x1=pupil_d(1,ond-1);
%             x2=pupil_d(2,ond-1);
%             y1=pupil_d(3,ond-1);
%             y2=pupil_d(4,ond-1);
%             vert=pupil_pos(1,ond-1);
%             horz=pupil_pos(2,ond-1);
%             blink_inds(ond)=ond;
%         else
%             x1=1;
%             x2=1;
%             y1=1;
%             y2=1;
%             vert=round(size(idata,2)/2);
%             horz=round(size(idata,1)/2);
%         end
%     else
%         v1=find(v==max(v));
%         v(v1)=[];
%         v2=find(v==max(v));
%         v(v2)=[];
%         v3=find(v==max(v));
%         vert=round(mean([v1 v2 v3]));
%
%         h1=find(h==max(h));
%         h(h1)=[];
%         h2=find(h==max(h));
%         h(h2)=[];
%         h3=find(h==max(h));
%         horz=round(mean([h1' h2' h3']));
%
%         vert=vert+vert_lim-1;
%         horz=horz+horz_lim-1;
%         x1=find(new_idata(horz,vert:end)==0,1,'first');
% %         if isempty(x1)
% %             x1=0;
% %         else
% %             x1=x1+vert-1;
% %         end
%         x2=find(new_idata(horz,vert:-1:1)==0,1,'first');
% %         if isempty(x2)
% %             x2=0;
% %         else
% %             x2=vert-(x2-1);
% %         end
%         y1=find(new_idata(horz:end,vert)==0,1,'first');
% %         if isempty(y1)
% %             y1=0;
% %         else
% %             y1=y1+horz-1;
% %         end
%         y2=find(new_idata(horz:-1:1,vert)==0,1,'first');
% %         if isempty(y2)
% %             y2=0;
% %         else
% %             y2=horz-(y2-1);
% %         end
%         x1=x1+vert-1;
%         x2=vert-(x2-1);
%         y1=y1+horz-1;
%         y2=horz-(y2-1);
%     end
%     pupil_d(1,ond)=x1;
%     pupil_d(2,ond)=x2;
%     pupil_d(3,ond)=y1;
%     pupil_d(4,ond)=y2;
%     pupil_pos(1,ond)=vert;
%     pupil_pos(2,ond)=horz;
% %     if sum(ond==[1785:1820])
% %         figure;imagesc(new_idata)
% %         hold on
% %         line([vert vert],[1 size(new_idata,1)],'color','black','linewidth',2)
% %         line([1 size(new_idata,2)],[horz horz],'color','black','linewidth',2)
% %     end
%
% end
%
% 1
% % blink_inds(blink_inds==0)=[];